class CacheManager {
  constructor() {
    this.caches = []
    this.cacheObj = {}
    this.preCount = 10
  }

  init() {
    this.caches = []
    this.cacheObj = {}
  }

  setCache(id, cache) {
    if (Object.prototype.hasOwnProperty.call(this.cacheObj, id)) {
      return
    }
    this.cacheObj[id] = cache
    this.caches.push(cache)
  }

  getCache(id) {
    return this.cacheObj[id]
  }

  get lastCache() {
    const length = this.caches.length
    if (length === 0) {
      return null
    } else {
      return JSON.parse(JSON.stringify(this.caches[length - 1]))
    }
  }

  get estimateHeight() {
    const lastCache = this.lastCache
    if (!lastCache) {
      return 50
    } else {
      return Math.ceil((lastCache.top + lastCache.height) / this.caches.length)
    }
  }
}

export default new CacheManager()
